package appfuse.webapp.core;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import appfuse.framework.WebvegaBaseAction;
import appfuse.framework.database.SqlCommand;
import appfuse.framework.exceptions.BaseException;
import appfuse.framework.util.TokenUtil;
import appfuse.framework.util.json.*;

public class UpdateApInfo extends WebvegaBaseAction {	
	@Override
	public ActionForward executeAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws BaseException, JSONException, IOException
	{		
		BufferedReader r = new BufferedReader(new InputStreamReader(request.getInputStream()));
	    String strRet = r.readLine();	    
		//strRet = "{mac:\"d8:5d:4c:2d:de:67\", pwd:\"123456\"}";
		ApItemInfo itemInfo = new ApItemInfo();	     
		JSONObject obj = new JSONObject(strRet);
		if (obj.has("token") == false ||
			obj.has("mac") == false ||
			obj.has("pwd") == false ||
			obj.has("share") == false)
		{
			//数据有问题
			PrintWriter out = response.getWriter();	    
		    out.print("{result:-2}");			
		    return null;
		}
		String strToken = obj.getString("token");
		String strImei = "";
		if (obj.has("imei") == true)
			strImei = obj.getString("imei");
		itemInfo.strMac = obj.getString("mac");
		itemInfo.strPwd = obj.getString("pwd");
		itemInfo.nShare = obj.getInt("share");
		String strUserName = "";
	    if (strToken.length() > 0)
	    {
	    	strUserName = TokenUtil.VaildToken(strToken);	    	
	    }
	    int nRet = updateApPwd(strUserName, strImei, itemInfo);
	    int nScore = -1;
	    
	    if (strUserName.length() > 0)
	    {
	    	if (nRet == 1)
		    {
	    		nScore = addScore(strUserName, 1);
		    }
		    else
		    {
		    	nScore = addScore(strUserName, 10);
		    }
	    }
	    PrintWriter out = response.getWriter();	    
	    out.print("{result:1,score:"+nScore+"}");			
	    return null;
	}
	/*
	 * 更新ap信息，返回值1表示是新的ap信息，0表示验证ap信息
	 */
	private int updateApPwd(String strUserName, String strImei, ApItemInfo itemInfo)
	{		
		int nRet = 0;
		String sql = "call sp_update_apinfo(?, ?, ?, ?)";		
		SqlCommand sqlCommand = new SqlCommand();
		try 
		{
			sqlCommand.setSqlCommand(sql);
			sqlCommand.setString(1, strUserName);
			sqlCommand.setString(2, strImei);
			sqlCommand.setString(3, itemInfo.strMac);
			sqlCommand.setString(4, itemInfo.strPwd);
			sqlCommand.setInt(5, itemInfo.nShare);
			ResultSet rs = sqlCommand.executeQuery();
			if (rs.next())
			{
				nRet = rs.getInt("result");
			}			
		} 
		catch (SQLException ex) 
		{
			ex.printStackTrace();
		} 
		finally 
		{
			sqlCommand.close();
			sqlCommand = null;
		}
		return nRet;
	}
	
	private int addScore(String strUserName, int nScore)
	{
		int nRet = -1;
		String sql = "call sp_add_user_score(\""+strUserName + "\"," + nScore+");";		
		System.out.println(sql);
		SqlCommand sqlCommand = new SqlCommand();
		try 
		{
			sqlCommand.setSqlCommand(sql);
			ResultSet rs = sqlCommand.executeQuery();
			if (rs.next())
			{
				nRet = rs.getInt("score");
			}					
		} 
		catch (SQLException ex) 
		{
			ex.printStackTrace();
		} 
		finally 
		{
			sqlCommand.close();
			sqlCommand = null;
		}
		return nRet;
	}	
}
